package com.sherlocky.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.sherlocky.common.SecurityContextHolder;

import conf.AppContextHolder;

/**
 * 处理IOS下emoji表情
 * @author zhangcx
 *
 */
public class EmojiUtil {
	private static final String IMAGE_FOLDER = "static/images/emoji/";
	private static Map<String, String> exitEmojiImages = new HashMap<String, String>();
	private static String[] emojiUnicodes = new String[0];
	static{
		try {
			File classFolder = new File(AppContextHolder.class.getProtectionDomain().getCodeSource().getLocation().getPath());
			while(!classFolder.getName().equalsIgnoreCase("WEB-INF")) {
				classFolder = classFolder.getParentFile();
			}
			File webRoot = classFolder.getParentFile();
			File imageFolder = new File(webRoot, IMAGE_FOLDER);
			String[] images = imageFolder.list();
			for (String fileName : images) {
				if(!StringUtils.endsWith(fileName, ".png")) {
					continue;
				}
				String emojiUnicode = StringUtils.removeEndIgnoreCase(StringUtils.removeStart(fileName, "emoji"), ".png");
				emojiUnicode = convertUnicode(emojiUnicode);
				exitEmojiImages.put(emojiUnicode, fileName);
			}
			emojiUnicodes = exitEmojiImages.keySet().toArray(new String[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static String convertUnicode(String emo) {
		emo = emo.substring(emo.indexOf("_") + 1);
		if (emo.length() < 6) {
			return new String(Character.toChars(Integer.parseInt(emo, 16)));
		}
		String[] emos = emo.split("_");
		char[] char0 = Character.toChars(Integer.parseInt(emos[0], 16));
		char[] char1 = Character.toChars(Integer.parseInt(emos[1], 16));
		char[] emoji = new char[char0.length + char1.length];
		for (int i = 0; i < char0.length; i++) {
			emoji[i]  = char0[i];
		}
		for (int i = char0.length; i < emoji.length; i++) {
			emoji[i]  = char1[i - char0.length];
		}
		return new String(emoji);
	}
	
	/**
     * 检测是否有emoji字符
     * @param source
     * @return 一旦含有就抛出
     */
    public static boolean containsEmoji(String source) {
        if (StringUtils.isBlank(source)) {
            return false;
        }

        int len = source.length();

        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);

            if (isEmojiCharacter(codePoint)) {
                //do nothing，判断到了这里表明，确认有表情字符
                return true;
            }
        }

        return false;
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return !((codePoint == 0x0) ||
                (codePoint == 0x9) ||
                (codePoint == 0xA) ||
                (codePoint == 0xD) ||
                ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
                ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
    }
    
    public static String filter(String str) {
		if(!containsEmoji(str)) {
			return str;
		}
		
		for (String emojiUnicode : emojiUnicodes) {
			str = str.replaceAll(emojiUnicode, 
					"<img src=\"" + StringUtils.defaultIfEmpty(SecurityContextHolder.getContextPath(), null) + IMAGE_FOLDER + exitEmojiImages.get(emojiUnicode) + "\"/>");
		}
		return str;
	}
	
	public static void main(String[] args) {
		
	}
	
}
